# 3.16 and later is required so we can build CodeLite with PCH
cmake_minimum_required(VERSION 3.16)

# ##############################################################################
#
# CodeLite IDE cmake file Typical usage will be (build in release mode):
#
# Linux / macOS:
# -------------------
# ~~~
# mkdir build
# cd build
# cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release ..
# make -j$(nproc) install
# ~~~
#
# MSYS2
# -------------------
#
# ~~~
# mkdir build-release
# cd build-release
# cmake -G"MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release .. -DWXWIN=C:/msys64/path/to/wx-dir
# make -j$(nproc)
# ~~~
#
# Optional command line arguments:
#
# -DCMAKE_BUILD_TYPE=Release|Debug|DebugFull -DCOPY_WX_LIBS=1|0
# -DPREVENT_WX_ASSERTS=1|0 -DAUTOGEN_REVISION=1|0 -DWITH_PCH=1|0
# -DWITH_NATIVEBOOK=1|0 -DWITH_WXPATH=/path/to/different_wx-config/directory/
# -DMAKE_DEB=1|0 -DENABLE_SFTP=1|0 // When set to 1 Codelite is built with
# SFTP/Remoty support. -DWITH_WX_CONFIG=/path/to/wx-config
#
# Windows / MSYS options:
#
# -DWXWIN=<fullpath>                          //
# https://docs.codelite.org/build/build_wx_widgets/#windows
#
# ##############################################################################

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
include(OSXDeps)

# ##############################################################################
# Defaults
# ##############################################################################
project("CodeLite")

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

option(BUILD_TESTING "Build test executables" OFF)

# Set this option to "ON" in order to get CMake standard/expected behavior
set(RETAIN_CACHED_VALUES
    OFF
    CACHE BOOL "Compat option to unset certain cached variables")

if(UNIX AND NOT APPLE)
  if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
    # user did not pass -DCMAKE_INSTALL_PREFIX and this is the first time we are
    # executing cmake, the default is set to /usr/local and we do not want
    # that..
    set(CMAKE_INSTALL_PREFIX
        "/usr"
        CACHE PATH "Install prefix" FORCE)
  endif()
endif()

if(MINGW)
  set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install")
  set(CL_PREFIX "${CMAKE_INSTALL_PREFIX}")
  if(NOT DEFINED ENV{MSYS2_BASE})
    message(
      FATAL_ERROR
        "Set the environment variable MSYS2_BASE to point to the installation of your MSYS2. e.g. export MSYS2_BASE=C:/msys64"
    )
  endif()
  set(MSYS2_BASE $ENV{MSYS2_BASE})
elseif(UNIX AND NOT APPLE)
  if(CL_PREFIX)
    # user provided CL_PREFIX -> use it instead of CMAKE_INSTALL_PREFIX
    set(CMAKE_INSTALL_PREFIX "${CL_PREFIX}")
  else()
    # update CL_PREFIX to CMAKE_INSTALL_PREFIX
    set(CL_PREFIX "${CMAKE_INSTALL_PREFIX}")
  endif()
endif()

message(STATUS "CMAKE_INSTALL_PREFIX is set to ${CMAKE_INSTALL_PREFIX}")
set(CODELITE_VERSION "18.3.0")

# for now, we keep wxCrafter version to be the same as CodeLite version this
# allows for better support
set(WXCRAFTER_VERSION ${CODELITE_VERSION})

# Generate compile_commands.json file for code completion
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CL_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR})

include(UtilsHelper)
find_package(SQLite3)

if(NOT SQLite3_FOUND)
  if(UNIX)
    message(
      FATAL_ERROR
        " **** Could not find sqlite3. Please install libsqlite3-dev package")
  elseif(APPLE)
    message(
      FATAL_ERROR
        " **** Could not find sqlite3. Please install Sqlite3 via brew")
  elseif(MINGW)
    message(
      FATAL_ERROR
        " **** Could not find sqlite3. Please install sqlite3 package: pacman -S mingw-w64-clang-x86_64-sqlite3"
    )
  else()
    message(
      FATAL_ERROR
        " **** Could not find sqlite3. Please install sqlite3 development package"
    )
  endif()
endif()

find_package(BISON REQUIRED)

if(NOT BISON_FOUND)
  if(UNIX)
    message(
      FATAL_ERROR " **** Could not find flex. Please install bison package")
  elseif(APPLE)
    message(FATAL_ERROR " **** Could not find flex. Please install bison")
  elseif(MINGW)
    message(
      FATAL_ERROR
        " **** Could not find flex. Please install bison package: pacman -S bison"
    )
  else()
    message(
      FATAL_ERROR " **** Could not find bison. Please install bison package")
  endif()
endif()

find_package(FLEX REQUIRED)

if(NOT FLEX_FOUND)
  if(UNIX)
    message(
      FATAL_ERROR " **** Could not find flex. Please install flex package")
  elseif(APPLE)
    message(FATAL_ERROR " **** Could not find flex. Please install flex")
  elseif(MINGW)
    message(
      FATAL_ERROR
        " **** Could not find flex. Please install flex package: pacman -S flex"
    )
  else()
    message(
      FATAL_ERROR " **** Could not find flex. Please install flex package")
  endif()
endif()

set(IS_FREEBSD 0)
if(CMAKE_SYSTEM_NAME MATCHES "^.*BSD$|DragonFly")
  set(IS_FREEBSD 1)
  add_compile_definitions(_BSD_SOURCE FREEBSD)
endif()
set(IS_NETBSD 0)
set(BUILD_WXC 0)
set(CL_COPY_WX_LIBS 0)
set(WITH_SFTP 1)

# Create the post_install.cmake script (no append here)
write_file("${CMAKE_BINARY_DIR}/post_install.cmake"
           "message(STATUS \"Running post install script\")")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations"
)# Avoid very multiple warnings spam due to deprecated wx methods
if(UNIX AND NOT APPLE)
  set(CMAKE_CXX_FLAGS
      "${CMAKE_CXX_FLAGS} -isystem /usr/include/harfbuzz -isystem /usr/local/include/harfbuzz"
  ) # Needed for fedora 31 so far, but will spread. See
    # https://gitlab.kitware.com/cmake/cmake/issues/19531
endif()

if(UNIX
   AND NOT APPLE
   AND NOT CYGWIN)
  set(CMAKE_EXE_LINKER_FLAGS
      "${CMAKE_EXE_LINKER_FLAGS} -Wl,--disable-new-dtags")
  set(CMAKE_SHARED_LINKER_FLAGS
      "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--disable-new-dtags")
endif()

if(UNIX)
  # Configure CCache if available
  find_program(CCACHE_FOUND ccache)
  if(CCACHE_FOUND)
    set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
    set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
  endif(CCACHE_FOUND)
endif(UNIX)

if(UNIX)
  set(BUILD_DIRECTORY ${CMAKE_BINARY_DIR})
elseif(MINGW)
  set(BUILD_DIRECTORY ${CMAKE_BINARY_DIR})
else()
  set(BUILD_DIRECTORY $ENV{CD})
endif()

message("-- BUILD_DIRECTORY is set to ${BUILD_DIRECTORY}")

set(OS_NAME "WIN")
if(UNIX)
  execute_process(
    COMMAND uname -s
    OUTPUT_VARIABLE OS_NAME
    OUTPUT_STRIP_TRAILING_WHITESPACE)
endif()
message("-- OS name ${OS_NAME}")

if(APPLE)
  set(WX_COMPONENTS "std aui propgrid ribbon")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
else(APPLE)
  set(WX_COMPONENTS "std aui propgrid stc ribbon richtext")
endif()

# Appstream metadata
set(METAINFO_FILE
    "${CMAKE_SOURCE_DIR}/Runtime/org.codelite.codelite.metainfo.xml")
install(FILES ${METAINFO_FILE} DESTINATION share/metainfo)

if(WITH_WXPATH)
  set(ENV{PATH} ${WITH_WXPATH}:$ENV{PATH})
endif()
if(NOT RETAIN_CACHED_VALUES)
  unset(WITH_WXPATH CACHE)
endif()

if(WITH_WX_CONFIG)
  set(CL_WX_CONFIG ${WITH_WX_CONFIG})
else(WITH_WX_CONFIG)
  set(CL_WX_CONFIG wx-config)
endif()

if(UNIX AND NOT MINGW)
  execute_process(
    COMMAND which ${CL_WX_CONFIG}
    OUTPUT_VARIABLE WX_TOOL
    OUTPUT_STRIP_TRAILING_WHITESPACE)
  if(NOT WX_TOOL)
    message(
      FATAL_ERROR
        "\nNo functional wx_config script was found in your PATH.\nIs the wxWidgets development package installed?"
    )
  else()
    # cmake find_package() will try env var WX_CONFIG as wx-config tool path
    # before checking its builtin hardcoded naming conbinations for wx-config
    # tool
    set(ENV{WX_CONFIG} ${WX_TOOL})
    execute_process(
      COMMAND sh ${WX_TOOL} --version
      OUTPUT_VARIABLE WX_VERSION
      OUTPUT_STRIP_TRAILING_WHITESPACE)
    if("${WX_VERSION}" VERSION_LESS "3.1.6")
      message(
        FATAL_ERROR
          "I'm afraid your wxWidgets version is too old.\nBuilding CodeLite requires at least wxWidgets-3.1.6"
      )
    endif()

    if("${WX_VERSION}" VERSION_GREATER "3.3.1")
      set(wxWITH_ZOOM_NAVIGATOR OFF)
      message(STATUS "✅ MiniMap is available, excluding ZoomNavigator plugin")
    else()
      set(wxWITH_ZOOM_NAVIGATOR ON)
      message(STATUS "✅ Building with ZoomNavigator plugin")
    endif()

    if(MINGW)
      execute_process(
        COMMAND sh ${WX_TOOL} --debug=no --rescomp
        OUTPUT_VARIABLE WX_RC_FLAGS
        OUTPUT_STRIP_TRAILING_WHITESPACE)
      string(REGEX REPLACE "windres" "" WX_RC_FLAGS ${WX_RC_FLAGS})
      set(CMAKE_RC_FLAGS "${CMAKE_RC_FLAGS} ${WX_RC_FLAGS}")
      add_compile_definitions(__WXMSW__)
    endif(MINGW)
  endif()
  message("-- wx-config used is: ${WX_TOOL}")
  message("-- wxWidgets version is: ${WX_VERSION}")
  if(NOT APPLE AND NOT MINGW)
    # Is the wx we are using built on gtk2 or 3?
    execute_process(
      COMMAND ${WX_TOOL} --selected_config
      OUTPUT_VARIABLE WX_GTK_VERSION
      OUTPUT_STRIP_TRAILING_WHITESPACE)
    string(SUBSTRING "${WX_GTK_VERSION}" "3" "1" GTK_VERSION)
    message("-- gtk version is: ${GTK_VERSION}")
  endif()
endif()

# ##############################################################################
# Override defaults with user input
# ##############################################################################

set(CL_INSTALL_LIBDIR "lib")

if(NOT MINGW)
  if(NOT CL_PREFIX)
    # If the caller hasn't set his own destination, install to a multi-arch lib
    # dir if applicable
    if(CMAKE_VERSION VERSION_GREATER 2.8.7 AND (UNIX AND NOT APPLE))
      if(CMAKE_VERSION VERSION_GREATER 3.0.0)
        # Prior to this, afaict the GNUInstallDirs module worked whatever the
        # prefix Since, it looks at CMAKE_INSTALL_PREFIX which is /usr/local by
        # default, and refuses to run the multiarch-setting code unless it's
        # /usr/ So, partly to comply with the default documented above, & partly
        # for packaging, explicitly set it to /usr
        set(CMAKE_INSTALL_PREFIX "/usr")
      endif()
      include(GNUInstallDirs) # defines CMAKE_INSTALL_LIBDIR to lib or lib64 or
                              # whatever. Available since cmake 2.8.8
      set(CL_INSTALL_LIBDIR ${CMAKE_INSTALL_LIBDIR})
    endif()

    set(CL_PREFIX "/usr")
  endif(NOT CL_PREFIX)
endif()

# Define the PLUGINS_DIR variable
if(MINGW)
  # MinGW
  set(MINGW_INSTALL_PLUGINS_DIR "${CMAKE_INSTALL_PREFIX}/lib/codelite")
  set(MINGW_INSTALL_BIN "${CMAKE_INSTALL_PREFIX}/bin")
  set(MINGW_INSTALL_RESOURCES_DIR "${CMAKE_INSTALL_PREFIX}/share/codelite")
endif()

if(APPLE)
  set(PLUGINS_DIR
      ${CMAKE_BINARY_DIR}/codelite.app/Contents/SharedSupport/plugins)
  set(CL_INSTALL_BIN ${CMAKE_BINARY_DIR}/codelite.app/Contents/MacOS)
  set(CL_RESOURCES_DIR ${CMAKE_BINARY_DIR}/codelite.app/Contents/SharedSupport)
elseif(MINGW)
  set(PLUGINS_DIR "${MINGW_INSTALL_PLUGINS_DIR}")
  set(CL_INSTALL_BIN "${MINGW_INSTALL_BIN}")
  set(CL_RESOURCES_DIR "${MINGW_INSTALL_RESOURCES_DIR}")
else()
  # /usr/lib/codelite
  set(PLUGINS_DIR "${CL_PREFIX}/${CL_INSTALL_LIBDIR}/codelite")
  # /usr/bin
  set(CL_INSTALL_BIN "${CMAKE_INSTALL_PREFIX}/bin")
  # /usr/share/codelite
  set(CL_RESOURCES_DIR "${CMAKE_INSTALL_PREFIX}/share/codelite")
endif()

message(STATUS "CL_INSTALL_BIN is set to ${CL_INSTALL_BIN}")
message(STATUS "PLUGINS_DIR is set to ${PLUGINS_DIR}")

# Try to link-to or build lldb?
if(UNIX)
  set(WITH_LLDB 1)
else()
  set(WITH_LLDB 0)
endif()
if(ENABLE_LLDB MATCHES 0)
  set(WITH_LLDB 0)
  message("-- Disabling lldb support")
endif()
if(NOT RETAIN_CACHED_VALUES)
  unset(ENABLE_LLDB CACHE)
endif()

set(DISABLE_CXX 0)
if(PHP_BUILD MATCHES 1)
  set(DISABLE_CXX 1)
  add_compile_definitions(PHP_BUILD=1)
else()
  set(DISABLE_CXX 0)
endif()

# ##############################################################################
# Locate libssh
# ##############################################################################
# Enable SFTP support?
if(ENABLE_SFTP MATCHES 0)
  set(WITH_SFTP 0)
endif(ENABLE_SFTP MATCHES 0)

if(MAKE_RPM_SLIM MATCHES 1)
  set(WITH_SFTP 0)
endif()

if(NOT RETAIN_CACHED_VALUES)
  unset(ENABLE_SFTP CACHE)
endif()

include(OSXInstall)
if(APPLE)
  execute_process(
    COMMAND wx-config --version
    OUTPUT_VARIABLE WX_VERSION
    OUTPUT_STRIP_TRAILING_WHITESPACE)
  if("${WX_VERSION}" VERSION_GREATER "3.3.1")
    set(wxWITH_ZOOM_NAVIGATOR OFF)
    message(STATUS "✅ MiniMap is available, excluding ZoomNavigator plugin")
  else()
    set(wxWITH_ZOOM_NAVIGATOR ON)
    message(STATUS "✅ Building with ZoomNavigator plugin")
  endif()
endif()

if(WITH_SFTP)
  if(UNIX OR MINGW)
    # Linux / MinGW / Apple / CYGWIN
    if(MINGW OR CYGWIN)
      find_library(
        LIBSSH_LIB
        NAMES ssh
        PATH_SUFFIXES lib)
      find_path(
        LIBSSH_INCLUDE_DIR
        NAMES libssh.h
        PATH_SUFFIXES include/libssh)
    elseif(APPLE)
      message("-- LIBSSH_INCLUDE_DIR is set to ${LIBSSH_INCLUDE_DIR}")
    elseif(UNIX)
      # Linux
      find_library(
        LIBSSH_LIB
        NAMES libssh.so
        HINTS /usr/local/lib /usr/lib ${CMAKE_INSTALL_LIBDIR})
      find_path(
        LIBSSH_INCLUDE_DIR
        NAMES libssh.h
        HINTS /usr/local/include /usr/include
        PATH_SUFFIXES libssh)
    endif()
    if(NOT APPLE)
      string(FIND ${LIBSSH_INCLUDE_DIR} "NOTFOUND" LIBSSH_NOT_FOUND_POS)
      if(LIBSSH_NOT_FOUND_POS GREATER -1)
        if(UNIX AND NOT APPLE)
          # Linux / FreeBSD
          message(FATAL_ERROR "**** NOTICE: Install libssh-dev and try again")
        elseif(NOT MINGW)
          # FreeBSD ?
          message(FATAL_ERROR "**** NOTICE: Install libssh and try again")
        endif()
        message(FATAL_ERROR "-- Could not find libssh")
      endif()
    endif()
  endif()
  message("-- LIBSSH_LIB is set to ${LIBSSH_LIB}")
endif(WITH_SFTP)

# On UNIX we require GTK
if(UNIX AND NOT APPLE)
  if(GTK_VERSION EQUAL 3)
    set(OLD_CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}) # Cache the current value
    set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
                          "${CMAKE_SOURCE_DIR}/cmake/Modules/")
    find_package(GTK3)
    set(CMAKE_MODULE_PATH ${OLD_CMAKE_MODULE_PATH}) # Reset, else the official
                                                    # path isn't used again :/
    if(GTK3_FOUND)
      include_directories(${GTK3_INCLUDE_DIRS})
    else(GTK3_FOUND)
      message(FATAL_ERROR "Could not locate GTK.")
    endif(GTK3_FOUND)
  else()
    find_package(GTK2)
    if(GTK2_FOUND)
      include_directories(${GTK2_INCLUDE_DIRS})
    else(GTK2_FOUND)
      message(FATAL_ERROR "Could not locate GTK.")
    endif(GTK2_FOUND)
  endif()

endif(UNIX AND NOT APPLE)

# find wxWidgets once
if(MINGW)
  if(NOT WXWIN)
    message(FATAL_ERROR "Missing -DWXWIN=<wxWidgets/root/install/dir>")
  endif()
  if(NOT WXCFG)
    set(WXCFG "clang_x64_dll/mswu")
  endif()

  message(STATUS "WXWIN is set to ${WXWIN}")
  message(STATUS "WXCFG is set to ${WXCFG}")

  execute_process(
    COMMAND ${CL_WX_CONFIG} --cmake --prefix=${WXWIN} --wxcfg=${WXCFG}
    OUTPUT_VARIABLE wxWidgets_USE_FILE
    OUTPUT_STRIP_TRAILING_WHITESPACE)
  message(STATUS "wxWidgets_USE_FILE is set to: ${wxWidgets_USE_FILE}")

  find_file(
    WX_VERSION_H_FILE
    NAMES wx/version.h REQUIRED
    HINTS ${WXWIN}
    PATH_SUFFIXES include)

  # Call the function – it will create the variables WX_MAJOR and WX_MINOR.
  extract_wx_version(
    "${WX_VERSION_H_FILE}" WX_MAJOR # <-- holds the major version
    WX_MINOR # <-- holds the minor version
  )

  math(EXPR wxVERSION "${WX_MAJOR} * 1000 + ${WX_MINOR} * 100")
  message(STATUS "wxVERSION: ${WX_MAJOR}.${WX_MINOR} (${wxVERSION})")
  if(wxVERSION GREATER_EQUAL 3320)
    set(wxWITH_ZOOM_NAVIGATOR OFF)
    message(STATUS "✅ MiniMap is available, excluding ZoomNavigator plugin")
  else()
    set(wxWITH_ZOOM_NAVIGATOR ON)
    message(STATUS "✅ Building with ZoomNavigator plugin")
  endif()

else()
  find_package(
    wxWidgets
    COMPONENTS adv
               aui
               base
               core
               html
               propgrid
               xml
               xrc
               net
               stc
               ribbon
               richtext
    REQUIRED)
endif()

if(AUTOGEN_REVISION MATCHES 0)
  set(MAKE_AUTOGEN_REVISION_STRING 0)
else(AUTOGEN_REVISION MATCHES 1)
  set(MAKE_AUTOGEN_REVISION_STRING 1)
endif(AUTOGEN_REVISION MATCHES 0)
if(NOT RETAIN_CACHED_VALUES)
  unset(AUTOGEN_REVISION CACHE)
endif()

set(BUILD_WXC 1)
if(UNIX AND NOT APPLE)
  set(WX_COMPONENTS "std aui propgrid stc richtext ribbon")
endif(UNIX AND NOT APPLE)

# package the wx libs?
if(COPY_WX_LIBS MATCHES 1)
  set(CL_COPY_WX_LIBS 1)
endif()
if(NOT RETAIN_CACHED_VALUES)
  unset(COPY_WX_LIBS CACHE)
endif()

# file encoding detect
if(UNIX
   AND NOT APPLE
   OR MINGW)
  # Linux
  if(MINGW)
    find_library(
      LIBUCHARDET_LIB
      NAMES uchardet
      PATH_SUFFIXES lib)
    find_path(
      LIBUCHARDET_INCLUDE_DIR
      NAMES uchardet.h
      PATH_SUFFIXES include/uchardet)
  else()
    find_library(
      LIBUCHARDET_LIB
      NAMES libuchardet.so
      HINTS /usr/local/lib /usr/lib ${CMAKE_INSTALL_LIBDIR})
    find_path(
      LIBUCHARDET_INCLUDE_DIR
      NAMES uchardet.h
      HINTS /usr/local/include /usr/include
      PATH_SUFFIXES uchardet)
  endif()
else(
  UNIX
  AND NOT APPLE
  OR MINGW)
  # OSX set( LIBUCHARDET_INCLUDE_DIR ${CL_SRC_ROOT}/sdk/uchardet/include) set(
  # LIBUCHARDET_LIB ${CL_SRC_ROOT}/sdk/uchardet/lib/osx/uchardet.a)
endif(
  UNIX
  AND NOT APPLE
  OR MINGW)
if(LIBUCHARDET_LIB AND LIBUCHARDET_INCLUDE_DIR)
  add_compile_definitions(USE_UCHARDET=1)
  include_directories(${LIBUCHARDET_INCLUDE_DIR})
else(
  UNIX
  AND NOT APPLE
  OR MINGW)
  set(LIBUCHARDET_LIB "")
endif(LIBUCHARDET_LIB AND LIBUCHARDET_INCLUDE_DIR)

# Under OSX, create the skeleton bundle directory
osx_make_bundle_directory()

add_compile_definitions(YY_NEVER_INTERACTIVE=1)

if(NOT MINGW)
  add_compile_definitions(INSTALL_DIR=\"${CL_PREFIX}/share/codelite\")
  add_compile_definitions(PLUGINS_DIR=\"${PLUGINS_DIR}\")
else()
  add_compile_definitions(NDEBUG)
endif()

message("-- PLUGINS_DIR is set to ${PLUGINS_DIR}")

# Allow user to use wxAuiNotebook instead of the native notebook
if(UNIX
   AND NOT APPLE
   AND NOT MINGW)
  # by default, use GTK native notebook control
  set(CL_GTK_USE_NATIVEBOOK 1)
  if(WITH_NATIVEBOOK MATCHES 0)
    set(CL_GTK_USE_NATIVEBOOK 0)
  endif()

  if(CL_GTK_USE_NATIVEBOOK MATCHES 1)
    add_compile_definitions(GTK_USE_NATIVEBOOK=1)
    message("-- Using Native Notebook class")
  else()
    add_compile_definitions(GTK_USE_NATIVEBOOK=0)
    message("-- Using custom Notebook")
  endif()
else() # NOT LINUX
  add_compile_definitions(GTK_USE_NATIVEBOOK=0)
  message("-- Using custom Notebook")
endif()

if(NOT RETAIN_CACHED_VALUES)
  unset(CL_GTK_USE_NATIVEBOOK CACHE)
endif()

# ##############################################################################
# Global optimizations
# ##############################################################################

if(CMAKE_BUILD_TYPE MATCHES Debug OR CMAKE_BUILD_TYPE MATCHES DebugFull)
  message("-- Building in ${CMAKE_BUILD_TYPE} mode")
  set(DEBUG_BUILD 1)
  set(CL_PCH_FILE "${CL_SRC_ROOT}/PCH/precompiled_header_dbg.h")
  set(CL_PCH_TARGET "precompiled_header_dbg.h.gch")
  set(BUILD_TYPE "Debug")

  # Set the libraries output directory
  set(CL_BIN_DIR bin)
  set(CL_LIB_DIR lib)
  if(APPLE)
    add_custom_target(distclean COMMAND cd ${CL_SRC_ROOT}/PCH && $(MAKE)
                                        type=debug clean)
  endif(APPLE)

  set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${BUILD_DIRECTORY}/${CL_LIB_DIR}")
  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${BUILD_DIRECTORY}/${CL_BIN_DIR}")
  set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${BUILD_DIRECTORY}/${CL_LIB_DIR}")
  set(CL_LIBPATH "${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}")

  message(
    "-- Executables will be written into ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
  message(
    "-- Shared Objects will be written into ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")

  if(APPLE OR MINGW)
    # Clang compiler on these platforms
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -gdwarf-4 -fstandalone-debug -O0"
    )# No optimization, debug info
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -gdwarf-4 -fstandalone-debug -O0")
    message(STATUS "Adding Clang debug flags: -gdwarf-4 -fstandalone-debug -O0")
  else()
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0") # No optimization, debug
                                                     # info
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0")
    message(STATUS "Adding GCC debug flags: -g -O0")
  endif()

  # In debug, only add NDEBUG if the user says so
  if(PREVENT_WX_ASSERTS MATCHES 1)
    message("-- Adding -DNDEBUG to definitions")
    add_compile_definitions(NDEBUG)
  endif()
  add_compile_definitions(CL_DEBUG_BUILD)
else()
  message("-- Building in Release mode")
  set(DEBUG_BUILD 0)
  add_compile_definitions(NDEBUG)
  add_compile_definitions(-DwxDEBUG_LEVEL=0)
  set(CMAKE_INSTALL_DO_STRIP TRUE)
  message("-- CMAKE_INSTALL_DO_STRIP is " ${CMAKE_INSTALL_DO_STRIP})
  if(UNIX AND NOT APPLE)
    # Avoid hardening-no-relro wrarning message from lintian
    if(EXISTS "/usr/bin/dpkg-buildflags")
      execute_process(
        COMMAND /usr/bin/dpkg-buildflags --get CFLAGS
        OUTPUT_VARIABLE EXTRA_CFLAGS
        OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
      execute_process(
        COMMAND /usr/bin/dpkg-buildflags --get CPPFLAGS
        OUTPUT_VARIABLE EXTRA_CPPFLAGS
        OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
      execute_process(
        COMMAND /usr/bin/dpkg-buildflags --get CXXFLAGS
        OUTPUT_VARIABLE EXTRA_CXXFLAGS
        OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
      execute_process(
        COMMAND /usr/bin/dpkg-buildflags --get LDFLAGS
        OUTPUT_VARIABLE EXTRA_LDFLAGS
        OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CXXFLAGS}")
      add_definitions(${EXTRA_CPPFLAGS})
      set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
      set(LINKER_OPTIONS "${EXTRA_LDFLAGS}")
      list(APPEND LINKER_OPTIONS "-s") # strip binaries
    endif()
  endif()

  set(CL_PCH_FILE "${CL_SRC_ROOT}/PCH/precompiled_header_release.h")
  set(CL_PCH_TARGET "precompiled_header_release.h.gch")
  if(APPLE)
    add_custom_target(distclean COMMAND cd ${CL_SRC_ROOT}/PCH && $(MAKE)
                                        type=release clean)
  endif(APPLE)

  # Set the libraries output directory
  set(CL_BIN_DIR bin)
  set(CL_LIB_DIR lib)

  set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${BUILD_DIRECTORY}/${CL_LIB_DIR}")
  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${BUILD_DIRECTORY}/${CL_BIN_DIR}")
  set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${BUILD_DIRECTORY}/${CL_LIB_DIR}")
  set(CL_LIBPATH "${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}")

  message(
    "-- Executables will be written into ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
  message(
    "-- Shared Objects will be written into ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")

  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2") # Optimize
  if(CMAKE_COMPILER_IS_GNUCXX)
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s") # Strip binary
  endif(CMAKE_COMPILER_IS_GNUCXX)

  # In release, add NDEBUG unless explicitly told not to
  if(NOT PREVENT_WX_ASSERTS MATCHES 0)
    message("-- Adding -DNDEBUG to definitions")
    add_compile_definitions(NDEBUG)
  endif()

endif()
if(NOT RETAIN_CACHED_VALUES)
  unset(CMAKE_BUILD_TYPE CACHE)
  unset(PREVENT_WX_ASSERTS CACHE)
endif()

# ##############################################################################
# Determine if 32 or 64 bit
# ##############################################################################

set(ARCH 32)
set(ARCH_NAME i386)
set(OS_CODENAME "unknown")

if(UNIX AND NOT APPLE)
  if(CMAKE_SYSTEM_NAME MATCHES "^.*BSD$|DragonFly")
    set(OS_CODENAME "${OS_NAME}")
    set(OS_DISTRO "${OS_NAME}")
  else()
    execute_process(
      COMMAND /bin/sh -c "lsb_release -a|grep -i Codename | cut -d: -f2"
      OUTPUT_VARIABLE OS_CODENAME
      OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
    execute_process(
      COMMAND /bin/sh -c "lsb_release -a|grep -i Distributor | cut -d: -f2"
      OUTPUT_VARIABLE OS_DISTRO
      OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
  endif()
  execute_process(
    COMMAND /bin/sh -c "uname -m"
    OUTPUT_VARIABLE OS_ARCH
    OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)

  # replace any tab/space with nothing
  string(REPLACE " " "" DISTRO_CODENAME
                 "${OS_DISTRO}-${OS_CODENAME}-${OS_ARCH}")
  string(REPLACE "\t" "" DISTRO_CODENAME "${DISTRO_CODENAME}")
  string(TOLOWER ${DISTRO_CODENAME} DISTRO_CODENAME)

  string(REPLACE " " "" OS_CODENAME "${OS_CODENAME}")
  string(REPLACE "\t" "" OS_CODENAME "${OS_CODENAME}")
  if(NOT "${OS_CODENAME}" STREQUAL "") # It'll be empty if lsb_release isn't
                                       # installed, which causes cmake to error
                                       # out on the next line
    string(TOLOWER ${OS_CODENAME} OS_CODENAME)
  endif()

  set(CPACK_SYSTEM_NAME "${DISTRO_CODENAME}")
  message("-- CPACK_SYSTEM_NAME ${CPACK_SYSTEM_NAME}")
endif(UNIX AND NOT APPLE)
message("-- OS_CODENAME is set to ${OS_CODENAME}")

if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  set(ARCH 64)
endif()
set(ARCH_NAME ${CMAKE_HOST_SYSTEM_PROCESSOR})
message("-- ARCH ${ARCH}")
message("-- ARCH_NAME ${ARCH_NAME}")

# ##############################################################################
# CPack
# ##############################################################################
if(MAKE_DEB)
  get_distro_name(distro)
  message("-- Distro name: ${distro}")

  set(DEB_DEPS "")
  if(${distro} STREQUAL "debian_10")
    set(DEB_DEPS "clang-tools (>= 7), clang-format (>= 10)")
  endif()

  if(${distro} MATCHES "ubuntu")
    set(DEB_DEPS "clangd (>= 10), clang-format (>= 10)")
  endif()

  message("-- Generating deb target")
  if(${ARCH} EQUAL 32)
    message("-- CPACK_DEBIAN_PACKAGE_ARCHITECTURE i386")
    set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "i386")
  else()
    message("-- CPACK_DEBIAN_PACKAGE_ARCHITECTURE amd64")
    set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "amd64")
  endif()

  if(GTK_VERSION EQUAL 3)
    set(LIBGTK "libgtk-3-dev")
    set(GTKNAME "gtk3")
  else()
    set(LIBGTK "libgtk2.0-0")
    set(GTKNAME "gtk2")
  endif()
  if(PHP_BUILD)
    set(GTKNAME "${GTKNAME}-php")
  endif()
  set(CPACK_GENERATOR "DEB")
  set(CPACK_PACKAGE_NAME "CodeLite")
  set(CPACK_PACKAGE_VERSION "${CODELITE_VERSION}-${GTKNAME}")
  set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Eran Ifrah <eran@codelite.org>")
  set(CPACK_PACKAGE_DESCRIPTION_SUMMARY
      "C/C++/Rust/Python/PHP/Node.js IDE (Integrated Development Environment)")
  set(CPACK_DEBIAN_PACKAGE_SECTION "devel")
  set(CPACK_DEBIAN_PACKAGE_PRIORITY "optional")
  set(CPACK_DEBIAN_PACKAGE_RECOMMENDS
      "build-essential, git, subversion, gdb, xterm, gcc, g++")
  set(CPACK_DEBIAN_PACKAGE_DEPENDS "${DEB_DEPS}")
  set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
  set(CPACK_PACKAGE_DESCRIPTION_FILE "${CL_SRC_ROOT}/DESC")
  set(CPACK_STRIP_FILES TRUE)
  include(CPack)
endif(MAKE_DEB)

if(MAKE_RPM)
  message("-- Generating rpm target")
  message("-- CPACK_PACKAGE_ARCHITECTURE amd64")
  if(GTK_VERSION EQUAL 3)
    set(GTKNAME "gtk3")
  else()
    set(GTKNAME "gtk2")
  endif()
  set(CPACK_RPM_PACKAGE_AUTOREQ 0)
  set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION
      "/usr/share/icons;/usr/share/locale/;/usr/share/locale/cs;/usr/share/locale/cs/LC_MESSAGES;/usr/share/locale/ja_JP;/usr/share/locale/ja_JP/LC_MESSAGES;/usr/share/locale/ru_RU;/usr/share/locale/ru_RU/LC_MESSAGES;/usr/share/locale/zh_CN;/usr/share/locale/zh_CN/LC_MESSAGES;/usr/share/man;/usr/share/icons/hicolor;/usr/share/icons/hicolor/128x128;/usr/share/icons/hicolor/128x128/apps;/usr/share/icons/hicolor/256x256;/usr/share/icons/hicolor/256x256/apps;/usr/share/icons/hicolor/32x32;/usr/share/icons/hicolor/32x32/apps;/usr/share/icons/hicolor/64x64;/usr/share/icons/hicolor/64x64/apps;/usr/share/applications"
  )
  set(CPACK_RPM_PACKAGE_REQUIRES "gcc, g++, git, sqlite, libssh, gtk3, cmake")
  set(CPACK_GENERATOR "RPM")
  set(CPACK_RPM_PACKAGE_NAME "CodeLite")
  set(CPACK_RPM_FILE_NAME "CodeLite-${CODELITE_VERSION}-x86_64.rpm")
  set(CPACK_RPM_PACKAGE_VERSION "${CODELITE_VERSION}-${GTKNAME}")
  set(CPACK_PACKAGE_CONTACT "Eran Ifrah <eran@codelite.org>")
  set(CPACK_PACKAGE_DESCRIPTION_SUMMARY
      "C/C++/Rust/Python/PHP/Node.js IDE (Integrated Development Environment)")
  set(CPACK_PACKAGE_VENDOR "The CodeLite Team")
  set(CPACK_STRIP_FILES TRUE)
  include(CPack)
endif(MAKE_RPM)

if(MAKE_RPM_SLIM)
  message("-- Generating rpm target")
  message("-- CPACK_PACKAGE_ARCHITECTURE amd64")
  if(GTK_VERSION EQUAL 3)
    set(GTKNAME "gtk3")
  else()
    set(GTKNAME "gtk2")
  endif()
  set(CPACK_RPM_PACKAGE_AUTOREQ 0)
  set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION
      "/usr/share/icons;/usr/share/locale/;/usr/share/locale/cs;/usr/share/locale/cs/LC_MESSAGES;/usr/share/locale/ja_JP;/usr/share/locale/ja_JP/LC_MESSAGES;/usr/share/locale/ru_RU;/usr/share/locale/ru_RU/LC_MESSAGES;/usr/share/locale/zh_CN;/usr/share/locale/zh_CN/LC_MESSAGES;/usr/share/man;/usr/share/icons/hicolor;/usr/share/icons/hicolor/128x128;/usr/share/icons/hicolor/128x128/apps;/usr/share/icons/hicolor/256x256;/usr/share/icons/hicolor/256x256/apps;/usr/share/icons/hicolor/32x32;/usr/share/icons/hicolor/32x32/apps;/usr/share/icons/hicolor/64x64;/usr/share/icons/hicolor/64x64/apps;/usr/share/applications"
  )
  set(CPACK_RPM_PACKAGE_REQUIRES "gcc72, g++72, git, sqlite, gtk3, cmake")
  set(CPACK_GENERATOR "RPM")
  set(CPACK_RPM_PACKAGE_NAME "CodeLite")
  set(CPACK_RPM_FILE_NAME "CodeLite-slim-${CODELITE_VERSION}-gtk2-x86_64.rpm")
  set(CPACK_RPM_PACKAGE_VERSION "${CODELITE_VERSION}-${GTKNAME}")
  set(CPACK_PACKAGE_CONTACT "Eran Ifrah <eran@codelite.org>")
  set(CPACK_PACKAGE_DESCRIPTION_SUMMARY
      "C/C++ IDE (Integrated Development Environment)")
  set(CPACK_PACKAGE_VENDOR "Eran Ifrah")
  set(CPACK_STRIP_FILES TRUE)
  include(CPack)
endif(MAKE_RPM_SLIM)

# ##############################################################################
# SFTP support
# ##############################################################################
if(WITH_SFTP)

  # Default is set to 1
  add_compile_definitions(USE_SFTP=1)
  message("-- USE_SFTP is set to 1")

else(WITH_SFTP)
  add_compile_definitions(USE_SFTP=0)
  message("-- USE_SFTP is set to 0")
  message("-- *** NOTICE ***: SFTP support is disabled ")

endif(WITH_SFTP)

# ##############################################################################
# RPATH settings
# ##############################################################################

if(UNIX AND NOT APPLE)
  set(CMAKE_SKIP_BUILD_RPATH TRUE)
  set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
  set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
  list(PREPEND CMAKE_INSTALL_RPATH "${PLUGINS_DIR}")
endif(UNIX AND NOT APPLE)

if(WITH_PCH)
  set(USE_PCH 1)
endif(WITH_PCH)
if(NOT RETAIN_CACHED_VALUES)
  unset(WITH_PCH CACHE)
endif()

if(MINGW AND USE_PCH)
  if(CMAKE_VERSION VERSION_LESS 3.16)
    message(
      FATAL_ERROR
        " **** MINGW and PCH requires at least cmake version 3.16 **** ")
  endif()
endif()

# add the dtl module include path before we include Plugin folder
include_directories(submodules/dtl)

if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES
                                            "AppleClang")
  set(IS_CLANG 1)
else()
  set(IS_CLANG 0)
endif()

if(IS_CLANG)
  add_compile_options(-Wthread-safety
                      -D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS)
endif()

# Extra lexers required by CodeLite
add_subdirectory(submodules/lexilla)

add_subdirectory(submodules)
include_directories(submodules/cJSON)

add_subdirectory(sdk/wxsqlite3)
add_subdirectory(sdk/databaselayer)

if(NOT APPLE)
  add_subdirectory(submodules/yaml-cpp)
  set(LIBYAML_CPP "yaml-cpp")
endif()

if(DEBUG_BUILD)
  set(CMAKE_CXX_FLAGS_RELEASE "")
  message(STATUS "CMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE}")
endif()

add_compile_definitions(wxUSE_GUI=1) # Missing `#include <wx/setup.h>` before
                                     # `#if wxUse_GUI` for some generated files

add_subdirectory(CxxParser)
add_subdirectory(CodeLite)
add_subdirectory(Plugin)
if(USE_PCH)
  add_subdirectory(PCH)
endif()

set(WITHOUT_INSTALL ON)
add_subdirectory(submodules/cc-wrapper)

# include the yaml-cpp directory
include_directories("${CL_SRC_ROOT}/submodules/yaml-cpp/include")
if(UNIX)
  # On Windows, this is default. Set it OFF for Linux / macOS as well
  set(BUILD_SHARED_LIBS OFF)
endif()

if(APPLE)
  set(CMAKE_INSTALL_PREFIX ${CL_INSTALL_BIN})
  set(CMAKE_INSTALL_BINDIR "${CMAKE_INSTALL_PREFIX}")
endif()

set(ASSISTANTLIB_BUILD_EXAMPLE
    OFF
    CACHE BOOL "Do not build OllamLib example program")
set(ASSISTANTLIB_WITH_OPENSSL
    ON
    CACHE BOOL "Assistant lib is buil with TLS support")
add_subdirectory("${CL_SRC_ROOT}/submodules/assistant")

if(WXC_APP)
  add_subdirectory(wxcrafter)
else()
  add_subdirectory(submodules/ctags)
  install(TARGETS ctags DESTINATION ${CL_INSTALL_BIN})
  if(MINGW)
    # build wx-config for Windows
    add_subdirectory(submodules/wx-config-msys2)
    install(TARGETS wx-config DESTINATION ${CL_INSTALL_BIN})
  endif()

  if(NOT NO_CORE_PLUGINS)
    add_subdirectory(abbreviation)
    if(NOT DISABLE_CXX)
      add_subdirectory(Gizmos)
      add_subdirectory(gdbparser)
      add_subdirectory(Debugger)
      add_subdirectory(UnitTestCPP)
      add_subdirectory(QmakePlugin)
      add_subdirectory(cppchecker)
      add_subdirectory(wxformbuilder)
      add_subdirectory(CMakePlugin)
      add_subdirectory(cscope)
      add_subdirectory(EOSWiki)
      add_subdirectory(ContinuousBuild)
    endif()
    add_subdirectory(AutoSave)
    add_subdirectory(CodeLiteDiff)
    add_subdirectory(git)
    add_subdirectory(Outline)
    add_subdirectory(CodeFormatter)
    add_subdirectory(Copyright)
    add_subdirectory(DatabaseExplorer)
    add_subdirectory(ExternalTools)
    add_subdirectory(SnipWiz)
    add_subdirectory(Subversion2)
    if(wxWITH_ZOOM_NAVIGATOR)
      message(STATUS "Including ZoomNavigator")
      add_subdirectory(ZoomNavigator)
    else()
      message(STATUS "Excluding ZoomNavigator")
    endif()
    add_subdirectory(SpellChecker)
    add_subdirectory(Tail)
    add_subdirectory(EditorConfigPlugin)
    add_subdirectory(PHPLint)
    add_subdirectory(PHPRefactoring)
    add_subdirectory(codelite_vim)
    add_subdirectory(Docker)
    add_subdirectory(LanguageServer)
    add_subdirectory(Rust)
    if(WITH_SFTP)
      add_subdirectory(SFTP)
      add_subdirectory(Remoty)
    endif(WITH_SFTP)
    add_subdirectory(DebugAdapterClient)
    add_subdirectory(codelitephp)
    add_subdirectory(WordCompletion)
    add_subdirectory(HelpPlugin)
    add_subdirectory(SmartCompletion)
    if(UNIX
       AND NOT APPLE
       AND NOT DISABLE_CXX)
      # Add valgrind plugin
      add_subdirectory(MemCheck)
    endif()

    if(APPLE AND NOT DISABLE_CXX)
      message("-- Adding MacBundler...")
      add_subdirectory(MacBundler)
    endif()
    add_subdirectory(CallGraph)
  endif(NOT NO_CORE_PLUGINS)
  add_subdirectory(wxcrafter)

  # Executables
  add_subdirectory(codelite_make)
  if(MINGW)
    # codelite-exec
    add_subdirectory(le_exec)

    # makedirs
    add_subdirectory(codelite_makedir)

  endif()

  # utility for importing themes
  add_subdirectory(codelite-generate-themes)

  add_subdirectory(codelite_echo)
  add_subdirectory(ctagsd)

  add_subdirectory(LiteEditor)
  if(NOT APPLE)
    add_subdirectory(translations)
  endif()
  add_dependencies(databaselayersqlite wxsqlite3)
  add_dependencies(wxshapeframework wxsqlite3)
  add_dependencies(libcodelite databaselayersqlite wxsqlite3)
  add_dependencies(plugin libcodelite)
  add_dependencies(codelite plugin)
  add_dependencies(codelite ctags)
  add_dependencies(codelite ctagsd)
  add_dependencies(codelite cc-wrapper)
  if(MINGW)
    add_dependencies(codelite wx-config)
  endif()
  if(NOT APPLE)
    add_dependencies(codelite yaml-cpp)
  endif()

  # Include our custom plugin.cmake module
  include(plugin)

  # Scan for user plugins
  cl_scan_for_plugins()
endif() # NOT WXC_APP

include(CTest)

message(STATUS "BUILD_TESTING=${BUILD_TESTING}")

if(BUILD_TESTING)
  add_subdirectory(CxxParserTests)
endif(BUILD_TESTING)

message(STATUS "CL_INSTALL_BIN is set to ${CL_INSTALL_BIN}")
install(TARGETS cc-wrapper DESTINATION ${CL_INSTALL_BIN})

if(NOT WXC_APP)
  # building CodeLite, install the SVGs
  codelite_install_svgs()

  if(NOT APPLE)
    set_target_properties(ctags PROPERTIES OUTPUT_NAME "codelite-ctags")
    install(
      TARGETS ctags
      DESTINATION ${CL_PREFIX}/bin
      PERMISSIONS ${EXE_PERM})
  elseif(APPLE)
    set_target_properties(ctags PROPERTIES OUTPUT_NAME "codelite-ctags")
    install(
      TARGETS ctags
      DESTINATION ${CMAKE_BINARY_DIR}/codelite.app/Contents/MacOS
      PERMISSIONS ${EXE_PERM})
  endif()

endif()

if(MINGW)
  # determine MSYS2 path
  write_file(${CMAKE_BINARY_DIR}/msys2-environment "export WXWIN=${WXWIN}")
  write_file(${CMAKE_BINARY_DIR}/msys2-environment
             "export MSYS2_HOME=${MSYS2_BASE}" APPEND)

  # install DLLs required by MinGW
  msys_list_deps(${MSYS2_BASE}/clang64/bin/libhunspell-1.7-0.dll
                 LIBHUNSPELL_DEPS)
  msys_list_deps(${MSYS2_BASE}/clang64/bin/libssh.dll LIBSSH_DEPS)
  msys_list_deps(${MSYS2_BASE}/clang64/bin/openssl.exe OPENSSL_DEPS)

  list(
    APPEND
    SYSTEM_DLLS
    libc++.dll
    libomp.dll
    libssh.dll
    libsqlite3-0.dll
    libhunspell-1.7-0.dll
    libunwind.dll)

  list(
    APPEND
    MSYS2_CORE_FILES
    msys-intl-8.dll
    msys-2.0.dll
    msys-iconv-2.dll
    rm.exe
    mkdir.exe
    cp.exe
    ls.exe
    mv.exe)

  execute_process(
    COMMAND sh -c
            'ls ${WXWIN}/lib/clang_x64_dll/wxmsw*u_clang*custom.dll|tail -1'
    OUTPUT_VARIABLE WXDLL
    OUTPUT_STRIP_TRAILING_WHITESPACE)

  if(NOT WXDLL)
    message(FATAL_ERROR "Could not locate wxWidgets DLL")
  endif()

  message(STATUS "Installing wxWidgets DLL ${WXDLL}")
  list(APPEND WXWIDGETS_DLLS ${WXDLL})
  list(APPEND CODELITE_RESOURCES ${CMAKE_SOURCE_DIR}/wxcrafter/wxgui.zip
       ${CMAKE_SOURCE_DIR}/LICENSE)

  foreach(DLL ${SYSTEM_DLLS})
    install(FILES "${MSYS2_BASE}/clang64/bin/${DLL}"
            DESTINATION "${CL_INSTALL_BIN}")
  endforeach()

  foreach(DLL ${MSYS2_CORE_FILES})
    install(FILES "${MSYS2_BASE}/usr/bin/${DLL}"
            DESTINATION "${CL_INSTALL_BIN}")
  endforeach()

  foreach(DLL ${WXWIDGETS_DLLS})
    install(FILES "${DLL}" DESTINATION "${CL_INSTALL_BIN}")
  endforeach()

  foreach(RESOURCE ${CODELITE_RESOURCES})
    install(FILES "${RESOURCE}" DESTINATION "${CMAKE_INSTALL_PREFIX}")
  endforeach()

  # copy extra deps dlls
  foreach(DLL ${LIBHUNSPELL_DEPS})
    install(FILES "${DLL}" DESTINATION "${CL_INSTALL_BIN}")
  endforeach()

  foreach(DLL ${LIBSSH_DEPS})
    install(FILES "${DLL}" DESTINATION "${CL_INSTALL_BIN}")
  endforeach()

  foreach(DLL ${OPENSSL_DEPS})
    install(FILES "${DLL}" DESTINATION "${CL_INSTALL_BIN}")
  endforeach()

endif()

if(MINGW AND NOT WXC_APP)
  # configure the innosetup file
  configure_file(${CL_SRC_ROOT}/InnoSetup/codelite64_mingw.iss.in
                 ${BUILD_DIRECTORY}/codelite64_mingw.iss)

  # CodeLite only related installation
  find_program(
    INNO_SETUP_CMP
    NAMES "ISCC" "ISCC.exe"
    PATHS "c:/Program Files (x86)/Inno Setup 6")
  if(NOT INNO_SETUP_CMP)
    message(
      WARNING
        "\n** Could not locate ISCC compiler! \"setup\" target is not created **"
    )
    message(
      WARNING
        "\n** You can install InnoSetup compiler from here: https://jrsoftware.org/ **\n"
    )
  else()
    message(STATUS "InnoSetup compiler is set to: ${INNO_SETUP_CMP}")

    # fake target that executes "install". we then use this target as the
    # dependency for the `setup` target
    add_custom_target(
      codelite-install COMMAND "${CMAKE_COMMAND}" --build "${BUILD_DIRECTORY}"
                               --target install)

    add_custom_target(
      setup
      COMMAND "${INNO_SETUP_CMP}" ${BUILD_DIRECTORY}/codelite64_mingw.iss
      WORKING_DIRECTORY "${CL_SRC_ROOT}/InnoSetup"
      DEPENDS codelite-install)
  endif()
elseif(MINGW AND WXC_APP)
  # configure the innosetup file
  configure_file(${CL_SRC_ROOT}/wxcrafter/wxcrafter64_mingw.iss.in
                 ${BUILD_DIRECTORY}/wxcrafter64_mingw.iss)

  # wxCrafter App
  find_program(
    INNO_SETUP_CMP
    NAMES "ISCC" "ISCC.exe"
    PATHS "c:/Program Files (x86)/Inno Setup 6")
  if(NOT INNO_SETUP_CMP)
    message(WARNING "** Could not locate ISCC compiler! **")
  else()
    message(STATUS "InnoSetup compiler is set to: ${INNO_SETUP_CMP}")
    add_custom_target(
      setup
      COMMAND "${INNO_SETUP_CMP}" ${BUILD_DIRECTORY}/wxcrafter64_mingw.iss
      WORKING_DIRECTORY "${CL_SRC_ROOT}/InnoSetup")
  endif()
endif()

unset(DEBUG_BUILD CACHE)
unset(BUILD_TESTING CACHE)

file(CREATE_LINK "${CMAKE_BINARY_DIR}/compile_commands.json"
     "${CMAKE_SOURCE_DIR}/compile_commands.json" SYMBOLIC)
